#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define ONE 1
#define TWO 1<<1
#define THREE 1<<2

void print(int flag)
{
    if((flag&ONE) == ONE)
        printf("1 ");
    if((flag&TWO) == TWO)
        printf("2 ");
    if((flag&THREE) == THREE)
        printf("3 ");
    printf("\n"); 
}
void test1()
{
    print(ONE);
    print(ONE|TWO);
    print(ONE|TWO|THREE);
}

void test2()
{
    int fd = open("log.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666);
    char buf[] = "hello file\n";
    write(fd, buf, sizeof(buf));     
    close(fd);
}

void test3()
{
    close(1);
    open("log.txt", O_WRONLY|O_APPEND);
    printf("aaaa\n");
}
void test4()
{
    char buf[64];
    close(0);
    open("log.txt", O_RDONLY);
    scanf("%s", buf);
    printf("%s", buf);
}

void test5()
{
    char buf[64];
    int fd = open("log.txt", O_RDONLY);
    ssize_t s = read(fd, buf, sizeof(buf));
    if(s > 0)
    {
        buf[s] = '\0';
    }
    printf(buf);
}
void test6()
{
    int fd = open("log.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666);
    dup2(fd, 1);
    printf("晚安\n");
}
int main()
{
    test5();
    return 0;
}
